package com.cn.medicalcare.service;

import com.cn.medicalcare.pojo.Admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginService {

    //静态内部类单例模式
    private static class SingletonClassInstance{
        private static final LoginService instance=new LoginService();
    }

    private LoginService(){}

    public static LoginService getInstance(){
        return LoginService.SingletonClassInstance.instance;
    }



    private AdminService adminService =AdminService.getInstance();

    public int isLogin(String username, String password, HttpServletRequest req){
        Admin admin = adminService.selectAdminByUsername(username);
        if(admin ==null){
            //用户名不存在
            return 0;
        }
        if(admin.getSurvival()==0){
            //用户未激活
            return 1;
        }
        if(!admin.getPassword().equals(adminService.encryption(password))){
            //密码错误
            return 2;
        }
        //密码验证通过
        //将用户信息存入session中
        HttpSession session=req.getSession();
        session.setAttribute("admin_user", admin);
        req.setAttribute("sessionId",session.getId());
        req.setAttribute("admin_user", admin);
        return 3;
    }
}
